// 创建一个基于对象的栈结构

// class Stack {
//     constructor () {
//       this.count = 0
//       this.items = []
//     }
// }


class Stack {
    constructor () {
      this.count = 0
      this.items = {}
    }
    push (element) {
      this.items[this.count] = element
      this.count++
    }
    size () {
      return this.count
    }
    isEmpty () {
      return this.count === 0
    }
    pop () {
      if (this.isEmpty()) {
        return undefined
      }
      const result = this.items[this.count--]
      delete this.items[this.count]
      return result
    }
    peek () {
      return this.items[this.count - 1]
    }
    clear () {
      this.count = 0
      this.items = {}
    }
    toString () {
      if (this.isEmpty()) {
        return ''
      }
      let result = this.items['0']
      for(let i = 1; i < this.count; i++) {
        result = `${result},${this.items[i]}`
      }
      return result
    }
}




let stack = new Stack()
console.log(stack.isEmpty())  // true
stack.push(1)
stack.push(3)
stack.push(5)
console.log(stack.size())     // 3
console.log(stack.peek())     // 5
console.log(stack.pop())      // 5
console.log(stack.toString()) // 1,3
stack.clear()
console.log(stack.size())     // 0
console.log(stack.isEmpty())  // true